.PHONY: all lib clean clean_obj userProg

CCFLAGS	:= -gstabs -I../ -nostdlib -nostdinc -fno-builtin 

LIBGCC	:= $(shell ${CC} -print-libgcc-file-name)

HEADERS	:= \
			kernel.h console.h \
			init.h mem.h intr.h pcs.h user.h task.h env.h sched.h\
			debug.h perm.h io.h iec.h kpipe.h ips.h \
			trapframe.h keyboard.h

OBJFILES:= kernel.S.o init.S.o kernel.o\
			init.o mem.o task.o env.o sched.o intr.o pcs.o io.o user.o debug.o\
			console.o perm.o ide.o iec.o kpipe.o ips.o \
			trapframe.o keyboard.o

all: kernel kernel.sym kernel.asm

lib:
	${MAKE} -C ../lib/

%.o: %.c ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

init.S.o: init.S ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

kernel.S.o : kernel.S userProg ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

userProg:
	${MAKE} -C ../user/
	../utils/bin2S/bin2S ../user/user userprog.S

kernel: lib ${OBJFILES}
	${LD} -N -T kernel.ld -o$@ ${OBJFILES} ../lib/*.o ${LIBGCC}

kernel.sym: kernel
	${NM} $< | grep -i " t " | sed "s/ [tT] / /g" > $@

kernel.asm: kernel
	${OBJDUMP} -S $< > $@

clean_obj:
	-${RM} ${OBJFILES}

clean: clean_obj
	-${RM} userprog.S
	-${MAKE} -C ../lib/ clean
	-${MAKE} -C ../user/ clean
	-${RM} kernel.sym
	-${RM} kernel
